package com.tjtt.gwm.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tjtt.common.utils.TotalFlowXmlTool;
import com.tjtt.gwm.bean.Junyans;
import com.tjtt.gwm.bean.Junyansceshis;
import com.tjtt.gwm.bean.Junyansshejis;
import com.tjtt.gwm.service.EquipmentService;
import com.tjtt.gwm.service.JunyansService;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.codeReview.service.ICodeReviewService;
import com.tjtt.tdm.flow.bean.FlowDetailed;
import com.tjtt.tdm.flow.service.IFlowformService;
import com.tjtt.tdm.util.PubMethod;
import com.tjtt.tdm.util.WordTool;

public class JunyansAction extends BaseAction {
	private EquipmentService epService;
	private JunyansService junyansService;
	private ICodeReviewService codeReviewService;
	private Junyans junyans;

	//-------------------------------------发起页面---------------------------------------
	/**
	 * 军研所列表
	 */
	public void queryJunyans(){
		try{
			String renwubh = getRequest().getParameter("renwubh");
			String chanpindh = getRequest().getParameter("chanpindh");
			String jieduanlx = getRequest().getParameter("jieduanlx");
			Map<String,String> filterMap=getPagerMap();
			if(null!=renwubh && !"".equals(renwubh)){
				filterMap.put("renwubh", StringUtil.encodingUrl(renwubh));
			}
			if(null!=chanpindh && !"".equals(chanpindh)){
				filterMap.put("chanpindh", StringUtil.encodingUrl(chanpindh));
			}
			if(null!=jieduanlx && !"".equals(jieduanlx)){
				filterMap.put("jieduanlx", StringUtil.encodingUrl(jieduanlx));
			}
			PageResult result= junyansService.queryJunyans(filterMap);
			String[] viewArray={"ID","renwubh","chanpindh","shiyanlx","fuzher","shiyandd","shemidj","shiyankssj","yujijssj","jieduanlx","jihuaydl"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	public void deleteJunyans() throws Exception{
		PrintWriter out=getResponse().getWriter();
		try{
			String sbId = getRequest().getParameter("ids");
			String ids[] = sbId.split(",");
			
			for(String id : ids){
				Junyans junyans = (Junyans) epService.getDataById(Junyans.class, Long.parseLong(id));
				epService.deleteData(junyans);
			}
		    out.print("{\"result\":\"success\"}");
		    out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		} 
	}
	
	public String initJunyansfq(){
		String junyansid = getRequest().getParameter("junyansid");
		if (junyansid==null||junyansid.equals("")){
			junyans = new Junyans();
			junyans.setRenwubh(getRenwubh());
			String shiyandg = this.getID();
			junyans.setShiyandg(Long.parseLong(shiyandg));
			epService.saveObject(junyans);
		}
		else{
			junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		}
		getRequest().setAttribute("shiyandg", junyans.getShiyandg());
		//流程相关
		this.initFlowaddBase();
		return "initJunyansfq";
	}
	
	public String getRenwubh(){
		String strSql = "select t.org_prex from sys_organization t where t.org_id='"+this.getOrgIds()+"' ";
		List orgprexlist = codeReviewService.queryListBySql(strSql);
		String strPrex = "";
		if (orgprexlist.size()>0&&orgprexlist.get(0)!=null&&!orgprexlist.get(0).toString().equals("")){
			strPrex = orgprexlist.get(0).toString();
		}
		else{
			strPrex = "JY";
		}
		Calendar a=Calendar.getInstance();
		String year = String.valueOf(a.get(Calendar.YEAR));
		String prefix = strPrex+"-"+year+"-";
		strSql = "select max(to_number(substr(t.renwubh,instr(t.renwubh,'-',1,2)+1,length(t.renwubh)-instr(t.renwubh,'-',1,2)))) from kl_junyans t where t.renwubh like '"+prefix+"%' ";
		List list = codeReviewService.queryListBySql(strSql);
		String renwubh = "";
		if(null != list.get(0) && !"".equals(list.get(0))){
			int number = Integer.parseInt(list.get(0).toString())+1;
			renwubh = renwubh + new DecimalFormat("0000").format(number);
		}else{
			renwubh = renwubh + "0001";
		}
		return prefix+renwubh;
	}
	
	
	public void getDaihaoinfo(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String datatypeid = getRequest().getParameter("datatypeid");
		String strSql = "select type_name,type_id from tdm_file_document_type where id='"+datatypeid+"' and levelindex=4";
		List lists = epService.queryDataBySql(strSql);
		if(lists.size()==0){
			out.print("{\"datatypeid\":\"\",\"daihao\":\"\",\"jieduanlx\":\"\"}");
		} 
		else{
			Object[] objs = (Object[])lists.get(0);
			String jieduanlx = objs[0].toString();
			String type_id = objs[1].toString();
			strSql = "select type_name from tdm_file_document_type where id='"+type_id+"' ";
			lists = epService.queryDataBySql(strSql);
			String daihao = lists.get(0).toString();
			out.print("{\"datatypeid\":\""+datatypeid+"\",\"daihao\":\""+daihao+"\",\"jieduanlx\":\""+jieduanlx+"\"}");
		}
		out.flush();
	}
	
	public void saveJunyans(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		epService.updateObject(junyans);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	public void submitJunyans(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		String taskinfoid = getRequest().getParameter("taskinfoid");
		junyans.setAddtime(new Timestamp(System.currentTimeMillis()));
		epService.updateObject(junyans);
		//流程相关
		if (taskinfoid!=null&&!taskinfoid.equals("")){
			this.saveUpdateBase("kl_junyans", junyansid);
		}
		else{
			this.saveFaqiBase("kl_junyans", junyansid);
		}
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	/**
	 * 发起页面设计所人员列表
	 */
	public void queryJunyansShejis(){
		try{
			String junyansid = getRequest().getParameter("junyansid");
			Map<String,String> filterMap=getPagerMap();
			if(null!=junyansid && !"".equals(junyansid)){
				filterMap.put("junyansid", StringUtil.encodingUrl(junyansid));
			}
			PageResult result= junyansService.queryJunyansshejis(filterMap);
			String[] viewArray={"ID","USERNAME","SEX","ZHICHENGNAME","ZHIWUNAME","ORG_NO","USERID"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	/**
	 * 发起页面设计所项目列表
	 */
	public void queryJunyansItem(){
		try{
			String junyansid = getRequest().getParameter("junyansid");
			Map<String,String> filterMap=getPagerMap();
			if(null!=junyansid && !"".equals(junyansid)){
				filterMap.put("junyansid", StringUtil.encodingUrl(junyansid));
			}
			PageResult result= junyansService.queryJunyansItem(filterMap);
			String[] viewArray={"ID","XIANGMUMC","BIAOZUNYQ","DANWEI","SHEBEIYQ","JIEGUO","YSJLPATH","REPORTPATH"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}

	}
	
	public void addJunyansShejis(){
		try{
			String userids = getRequest().getParameter("userids");
			String junyansid = getRequest().getParameter("junyansid");
			String[] useridarray = userids.split(",");
			
			for(int i=0;i<useridarray.length;i++){
				String strSql = "select 1 from kl_junyans_shejis t where t.junyansid='"+junyansid+"' and t.userid='"+useridarray[i]+"'";
				List lists = epService.queryDataBySql(strSql);
				if (lists.size()==0){
					Junyansshejis entity = new Junyansshejis();
					entity.setJunyansid(Long.parseLong(junyansid));
					entity.setUserid(Long.parseLong(useridarray[i]));
					epService.saveObject(entity);
				}
			}
			PrintWriter out = PubMethod.GetPrintWriter();
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}

	//-------------------------------------发起页面结束---------------------------------------
	
	//-------------------------------------添加测试室人员开始----------------------------------
	
	public String initCeshistjry(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		//流程相关
		this.initFlowupdateBase();
		return "initCeshistjry";
	}
	
	/**
	 * 发起页面设计所人员列表
	 */
	public void queryJunyansCeshis(){
		try{
			String junyansid = getRequest().getParameter("junyansid");
			Map<String,String> filterMap=getPagerMap();
			if(null!=junyansid && !"".equals(junyansid)){
				filterMap.put("junyansid", StringUtil.encodingUrl(junyansid));
			}
			PageResult result= junyansService.queryJunyansceshis(filterMap);
			String[] viewArray={"ID","USERNAME","SEX","ZHICHENGNAME","ZHIWUNAME","ORG_NO","USERID"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		
	}
	
	public void addJunyansCeshis(){
		try{
			String userids = getRequest().getParameter("userids");
			String junyansid = getRequest().getParameter("junyansid");
			String[] useridarray = userids.split(",");
			
			for(int i=0;i<useridarray.length;i++){
				String strSql = "select 1 from kl_junyans_ceshis t where t.junyansid='"+junyansid+"' and t.userid='"+useridarray[i]+"'";
				List lists = epService.queryDataBySql(strSql);
				if (lists.size()==0){
					Junyansceshis entity = new Junyansceshis();
					entity.setJunyansid(Long.parseLong(junyansid));
					entity.setUserid(Long.parseLong(useridarray[i]));
					epService.saveObject(entity);
				}
			}
			PrintWriter out = PubMethod.GetPrintWriter();
			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}
	
	public void submitCeshistjry(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		//流程相关
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------添加测试室人员结束----------------------------------
	
	//-------------------------------------添加测试室仪器开始----------------------------------
	
	public String initCeshistjyq(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initCeshistjyq";
	}
	
	/**
	 * 发起页面设计所人员列表
	 */
	public void queryJunyansCeshisyq(){
		try{
			String junyansid = getRequest().getParameter("junyansid");
			Map<String,String> filterMap=getPagerMap();
			if(null!=junyansid && !"".equals(junyansid)){
				filterMap.put("junyansid", StringUtil.encodingUrl(junyansid));
			}
			PageResult result= junyansService.queryJunyansceshisyq(filterMap);
			String[] viewArray={"ID","yiqilx","yiqimc","guigexh","zhizaocs","zhizaobh","jingdudj"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		
	}
	
	public void submitCeshistjyq(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------添加测试室人员结束----------------------------------
	
	
	//-------------------------------------试验节点开始---------------------------------------
	
	public String initShiyanjd(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initShiyanjdBase();
		return "initShiyanjd";
	}
	
	public void submitShiyanjd(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveShiyanjdBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------试验节点结束---------------------------------------
	
	//-------------------------------------填写原始记录报告节点开始-----------------------------
	
	public String initYsjlReport(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		String qitacsbg = this.getID();
		getRequest().setAttribute("qitacsbg", qitacsbg);
		this.initFlowupdateBase();
		return "initYsjlReport";
	}
	
	public void submitYsjlReport(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		String qitacsbg = getRequest().getParameter("qitacsbg");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		if (qitacsbg!=null&&!qitacsbg.equals("")){
			junyans.setQitacsbg(Long.parseLong(qitacsbg));
			epService.updateObject(junyans);
		}
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------填写原始记录报告节点结束----------------------------
	
	//-------------------------------------试验报告审核节点开始-------------------------------
	
	public String initShiyanbgsh(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initShiyanbgsh";
	}
	
	public void submitShiyanbgsh(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateFenzhiend("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------试验报告审核节点结束------------------------------
	
	//-------------------------------------填写理化申请单节点开始----------------------------
	
	public String initTianxielhsqd(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		if (junyans.getShenqingr()==null||junyans.getShenqingr().equals("")){
			junyans.setShenqingr(this.getUserNM());
			junyans.setShenqingrid(Long.parseLong(this.getUserId()));
		}
		if (junyans.getSongjianrq()==null||junyans.getSongjianrq().equals("")){
			junyans.setSongjianrq(PubMethod.getAdddate());
		}
		this.initFlowupdateBase();
		return "initTianxielhsqd";
	}
	
	public void saveTianxielhsqd(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		Junyans entity = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		entity.setWeituodw(junyans.getWeituodw());
		entity.setChanpinmc(junyans.getChanpinmc());
		entity.setGuige(junyans.getGuige());
		entity.setJianhao(junyans.getJianhao());
		entity.setYingduyq(junyans.getYingduyq());
		entity.setJiancelx(junyans.getJiancelx());
		entity.setShenqingr(junyans.getShenqingr());
		entity.setShenqingrid(junyans.getShenqingrid());
		entity.setSongjianrq(junyans.getSongjianrq());
		epService.updateObject(entity);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	public void checkHaslhsqb(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		Junyans entity = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		entity.setWeituodw(junyans.getWeituodw());
		entity.setChanpinmc(junyans.getChanpinmc());
		entity.setGuige(junyans.getGuige());
		entity.setJianhao(junyans.getJianhao());
		entity.setYingduyq(junyans.getYingduyq());
		entity.setJiancelx(junyans.getJiancelx());
		entity.setShenqingr(junyans.getShenqingr());
		entity.setShenqingrid(junyans.getShenqingrid());
		entity.setSongjianrq(junyans.getSongjianrq());
		epService.updateObject(entity);
		if (entity.getLihuasqb()==null||entity.getLihuasqb().equals("")){
			out.print("{\"result\":\"false\"}");
		}
		else{
			out.print("{\"result\":\"success\"}");
		}
	}
	
	public void checkSaved(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		Junyans entity = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		if (entity.getChanpinmc()==null||entity.getChanpinmc().equals("")){
			out.print("{\"result\":\"false\"}");
		}
		else{
			out.print("{\"result\":\"success\"}");
		}
	}

	public void getLihuasqbpath(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		Junyans entity = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		if (entity.getLihuasqb()==null||entity.getLihuasqb().equals("")){
			String pathUrl = getRequest().getSession().getAttribute("SYS_FILEPATH").toString();
			String path = pathUrl+"/lihuasqb/lihuasqb.docx";
			String targetPath = pathUrl+"/lihuasqb/temp/"+PubMethod.GetTimerName()+".docx";
			try {
				copyFile(new File(path),new File(targetPath));
				WordTool wt = new WordTool(targetPath);
				wt.SetBookMarkValue("委托单位", entity.getWeituodw());
				wt.SetBookMarkValue("产品名称", entity.getChanpinmc());
				wt.SetBookMarkValue("规格", entity.getGuige());
				wt.SetBookMarkValue("件号", entity.getJianhao());
				wt.SetBookMarkValue("硬度要求", entity.getYingduyq());
				wt.SetBookMarkValue("申请人", entity.getShenqingr());
				wt.SetBookMarkValue("送检日期", entity.getSongjianrq());
				wt.SetBookMarkValue("检测类型", entity.getJiancelx());
				wt.Save();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print("{\"path\":\""+targetPath+"\"}");
		}
		else{
			out.print("{\"path\":\""+entity.getLihuasqb()+"\"}");
		}
	}
	
	public void submitTianxielhsqb(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------填写理化申请单节点结束---------------------------
	
	//-------------------------------------军研所领导意见节点开始-----------------------------
	
	public String initLingdaoyj(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initLingdaoyj";
	}
	
	public void submitLingdaoyj(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------军研所领导意见节点结束---------------------------
	
	//-------------------------------------检验领导意见节点开始-----------------------------
	
	public String initJianyanldyj(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initJianyanldyj";
	}
	
	public void submitJianyanldyj(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------检验领导意见节点结束---------------------------
	
	
	//-------------------------------------理化主任确认节点开始-------------------------------
	
	public String initLihuazrqr(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initLihuazrqr";
	}
	
	public void getLihuabgpath(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		Junyans entity = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		if (entity.getLihuabg()==null||entity.getLihuabg().equals("")){
			String pathUrl = getRequest().getSession().getAttribute("SYS_FILEPATH").toString();
			String path = pathUrl+"/lihuasqb/lihuabg.docx";
			String targetPath = pathUrl+"/lihuasqb/temp/"+PubMethod.GetTimerName()+".docx";
			try {
				copyFile(new File(path),new File(targetPath));
				WordTool wt = new WordTool(targetPath);
				wt.SetBookMarkValue("委托单位", entity.getWeituodw());
				wt.SetBookMarkValue("产品名称", entity.getChanpinmc());
				wt.SetBookMarkValue("规格", entity.getGuige());
				wt.SetBookMarkValue("件号", entity.getJianhao());
				wt.SetBookMarkValue("硬度要求", entity.getYingduyq());
				wt.SetBookMarkValue("申请人", entity.getShenqingr());
				wt.SetBookMarkValue("送检日期", entity.getSongjianrq());
				wt.SetBookMarkValue("检测类型", entity.getJiancelx());
				wt.Save();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print("{\"path\":\""+targetPath+"\"}");
		}
		else{
			out.print("{\"path\":\""+entity.getLihuabg()+"\"}");
		}
	}
	
	public void submitLihuazrqr(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateFenzhiend("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------理化主任确认节点结束------------------------------
	
	//-------------------------------------理化主任确认节点开始-------------------------------
	public String initShiyanzj(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initShiyanzj";
	}
	
	public void getShiyanzjbgpath(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		Junyans entity = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		if (entity.getLihuasyzjbg()==null||entity.getLihuasyzjbg().equals("")){
			String pathUrl = getRequest().getSession().getAttribute("SYS_FILEPATH").toString();
			String path = pathUrl+"/lihuasqb/shiyanzjbg.docx";
			String targetPath = pathUrl+"/lihuasqb/temp/"+PubMethod.GetTimerName()+".docx";
			try {
				copyFile(new File(path),new File(targetPath));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print("{\"path\":\""+targetPath+"\"}");
		}
		else{
			out.print("{\"path\":\""+entity.getLihuasyzjbg()+"\"}");
		}
	}
	
	public void submitShiyanzj(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------理化主任确认节点结束------------------------------
	
	//-------------------------------------试验总结校对节点开始-------------------------------
	
	public String initShiyanzjjd(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initShiyanzjjd";
	}

	public void submitShiyanzjjd(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------试验总结校对节点结束------------------------------
	
	//-------------------------------------试验总结审核节点开始-------------------------------
	
	public String initShiyanzjsh(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initShiyanzjsh";
	}
	
	public void submitShiyanzjsh(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------试验总结审核节点结束------------------------------
	
	//-------------------------------------试验总结批准节点开始-------------------------------
	
	public String initShiyanzjpz(){
		String junyansid = getRequest().getParameter("mainid");
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		this.initFlowupdateBase();
		return "initShiyanzjpz";
	}

	public void submitShiyanzjpz(){
		PrintWriter out = PubMethod.GetPrintWriter();
		String junyansid = getRequest().getParameter("junyansid");
		String strSql = "update kl_yiqi t1 set t1.shiyongzt='维护' where t1.id in (select yiqiid from kl_junyans_yq where junyansid='"+junyansid+"')";
		epService.exeSql(strSql);
		this.saveUpdateBase("kl_junyans", junyansid);
		out.print("{\"result\":\"success\",\"junyansid\":\""+junyansid+"\"}");
	}
	
	//-------------------------------------试验总结审核节点结束------------------------------
	
	/**
	 * 查看流程信息
	 */
	public String viewFlowinfo(){
		String flowTaskId = getRequest().getParameter("flowTaskId");
		String junyansid = getRequest().getParameter("junyansid");
		String strSql = "";
		List lists = null;
		if (junyansid==null||junyansid.equals("")){
			strSql = "select main_id from flow_task where id='"+flowTaskId+"'";
			lists = codeReviewService.queryListBySql(strSql);
			junyansid = lists.get(0).toString();
		}
		junyans = (Junyans)epService.getDataById(Junyans.class, Long.parseLong(junyansid));
		return "viewFlowinfo";
	}
	
	public void getQianziinfo(){
		PrintWriter out = PubMethod.GetPrintWriter();
		out.print("{\"userid\":\""+this.getUserId()+"\",\"username\":\""+this.getUserNM()+"\",\"datetime\":\""+PubMethod.getAdddate()+"\"}");
	}
	
	/**
	 * 大纲页面列表
	 */
	public void queryShiyandg(){
		try{
			String dagangmc = getRequest().getParameter("dagangmc");
			String bianzhir = getRequest().getParameter("bianzhir");
			Map<String,String> filterMap=getPagerMap();
			if(null!=dagangmc && !"".equals(dagangmc)){
				filterMap.put("dagangmc", StringUtil.encodingUrl(dagangmc));
			}
			filterMap.put("userid", this.getUserId());
			PageResult result = junyansService.queryShiyandg(filterMap);
			String[] viewArray={"ID","dagangmc","bianzhir","bianzhirq","shemidj","junyansid","attachid","view_userid"};
			String str=getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	public void queryDataCenter() {
		try {
			PrintWriter out = PubMethod.GetPrintWriter(); 
			String nodeid = getRequest().getParameter("nodeid");
			String renwubh = getRequest().getParameter("renwubh");
			String chanpindh = getRequest().getParameter("chanpindh");
			String jieduanlx = getRequest().getParameter("jieduanlx");
			String starttime = getRequest().getParameter("starttime");
			String endtime = getRequest().getParameter("endtime");

			Map<String, String> filterMap = getPagerMap();
			if(!"".equals(nodeid)&&renwubh!=null){
				filterMap.put("nodeid", nodeid);
			}
			if(!"".equals(renwubh)&&renwubh!=null){
				renwubh = java.net.URLDecoder.decode(renwubh,"UTF-8");
				filterMap.put("renwubh", renwubh);
			}
			if(!"".equals(chanpindh)&&chanpindh!=null){
				chanpindh = java.net.URLDecoder.decode(chanpindh,"UTF-8");
				filterMap.put("chanpindh", chanpindh);
			}	
			if(!"".equals(jieduanlx)&&jieduanlx!=null){
				jieduanlx = java.net.URLDecoder.decode(jieduanlx,"UTF-8");
				filterMap.put("jieduanlx", jieduanlx);	
			}
			if(!"".equals(starttime)&&starttime!=null){
				filterMap.put("starttime", starttime);	
			}
			if(!"".equals(endtime)&&endtime!=null){
				filterMap.put("endtime", endtime);
			}
			String[] viewArray = { "junyansid", "renwubh","chanpindh","shiyanlx","fuzher","shiyandd",
								"shiyankssj","shiyanjssj","flow_state_name","add_user_name","add_time",
								"lihuasqb","lihuabg","lihuasyzjbg","dagangmc","attachid","qitacsbg"};
			// 查询所有的数据
			PageResult result = junyansService.queryDataCenter(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void addShiyandgviewuser(){
		try {
			PrintWriter out = PubMethod.GetPrintWriter(); 
			String userids = getRequest().getParameter("userids");
			String attachid = getRequest().getParameter("attachid");
			String strSql = "update TDM_ATTACHMENT set VIEW_USERID='"+userids+"' where ID='"+attachid+"'";
			epService.exeSql(strSql);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String viewqitacsbg(){
		String qitacsbg = getRequest().getParameter("qitacsbg");
		getRequest().setAttribute("qitacsbg", qitacsbg);
		return "viewqitacsbg";
	}
	
	/**
	 * 报告签名
	 */
	public void reportQianzi(){
		try {
			PrintWriter out = PubMethod.GetPrintWriter(); 
			String junyansid = getRequest().getParameter("junyansid");
			String qianzifield = getRequest().getParameter("qianzifield");
			String strSql = "update kl_junyans set "+qianzifield+"='"+this.getUserId()+"' where id='"+junyansid+"'";
			epService.exeSql(strSql);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查看报告是否签名
	 */
	public void CheckQianzi(){
		try {
			PrintWriter out = PubMethod.GetPrintWriter(); 
			String junyansid = getRequest().getParameter("junyansid");
			String qianzifield = getRequest().getParameter("qianzifield");
			String strSql = "select "+qianzifield+" from kl_junyans where id='"+junyansid+"'";
			List lists = codeReviewService.queryListBySql(strSql);
			if (lists.get(0)!=null&&!lists.get(0).toString().equals("")){
				out.print("{\"result\":\"success\"}"); 
			}
			else{
				out.print("{\"result\":\"failse\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getBackNodeList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/XML,charset=GBK");
		response.setHeader("Charset", "GBK");
		response.setCharacterEncoding("GBK");
		response.setContentType("text/xml");
		PrintWriter out = null;
		
		try{
			out = getResponse().getWriter();
			String taskinfoid = getRequest().getParameter("taskinfoid");
			String strSql = "select flow_task_id,flow_state_id from flow_task_info where id='"+taskinfoid+"'";
			List lists = codeReviewService.queryListBySql(strSql);
			Object[] objtask = (Object[])lists.get(0);
			strSql = "select distinct t.statusbeforeid,t.statusbefore from flow_detailed t where t.flow_task_id='"+objtask[0]+"' and statusbeforeid<>'"+objtask[1]+"'  ";
			List list = codeReviewService.queryListBySql(strSql);	
			//查询
			String returnStr = "[";
			if(list != null && list.size()>0){
				for(int i=0;i<list.size();i++){
					Object[] objs = (Object[])list.get(i);
					returnStr += "{\"ntid\":\""+objs[0]+"\",\"convalue\":\""+objs[1]+"\"},";
				}
				returnStr = returnStr.substring(0,returnStr.length()-1)+"]";
			}
			out.print(returnStr);
		}catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
		}
	}
	
	public void backFlow(){
		PrintWriter out = PubMethod.GetPrintWriter(); 
		try{
			String backnode = getRequest().getParameter("backnode");
			String taskinfoid = getRequest().getParameter("taskinfoid");
			String flowtaskid = getRequest().getParameter("flowtaskid");
			
			String strSql = "select t1.FLOW_STATE_ID,t1.FLOW_STATE_NAME,t2.table_name,t2.main_id from FLOW_TASK_INFO t1 " +
					"left join flow_task t2 on t1.flow_task_id=t2.id  where t1.id='"+taskinfoid+"'";
			List liststate = codeReviewService.queryListBySql(strSql);
			Object[] objstates = (Object[])liststate.get(0);
			strSql = "select STATUSBEFORE,t.operatorperson_id,t1.username from flow_detailed t " +
					"left join sys_users t1 on t.operatorperson_id=t1.userid " +
					"where t.flow_task_id='"+flowtaskid+"' and t.statusbeforeid='"+backnode+"'  order by OPERATORTIME desc";
			List lists = codeReviewService.queryListBySql(strSql);
			Object[] objs = (Object[])lists.get(0);
			
			if (backnode.equals("tianjiasjsry")){
				strSql = "delete from FLOW_TASK_INFO where FLOW_TASK_ID='"+flowtaskid+"'";
				epService.exeSql(strSql);
				strSql = "delete from flow_detailed where flow_task_id='"+flowtaskid+"'";
				epService.exeSql(strSql);
				strSql = "delete from flow_task where id='"+flowtaskid+"'";
				epService.exeSql(strSql);

				if(objstates[2].equals("kl_junyans")){
					strSql = "update kl_yiqi t1 set t1.shiyongzt='维护' where t1.id in (select yiqiid from kl_junyans_yq where junyansid='"+objstates[3]+"')";
					epService.exeSql(strSql);
				}
			}
			else{
				strSql = "update flow_task_info set FLOW_STATE_ID='"+backnode+"'," +
						"FLOW_STATE_NAME='"+objs[0]+"',NEXT_USERS_ID=',"+objs[1]+",'," +
						"NEXT_USERS_NAME='"+objs[2]+"' where id='"+taskinfoid+"'";
				epService.exeSql(strSql);
			}
			WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(getSession().getServletContext());
			IFlowformService flowformService =  (IFlowformService)factory.getBean("flowformService");
			FlowDetailed flowdetail = new FlowDetailed();
			flowdetail.setOperatorperson(this.getUserNM());
			flowdetail.setOperatorpersonId(Long.parseLong(this.getUserId()));
			flowdetail.setStatusbeforeid(objstates[0].toString());
			flowdetail.setStatusbefore(objstates[1].toString());
			flowdetail.setStatusafterid(backnode);
			flowdetail.setStatusafter(objs[0].toString());
			flowdetail.setDeptid(Long.parseLong(this.getDeptid()));
			flowdetail.setOperatortime(new Timestamp(System.currentTimeMillis()));
			flowdetail.setActiontype("打回");
			flowformService.saveFlowDetailed(flowdetail);

			out.print("{\"result\":\"success\"}");
		}
		catch(Exception exp){
			exp.printStackTrace();
			out.print("{\"result\":\"false\"}");
		}
	}
	
	/**
	 * 报告签名
	 */
	public void getNextSteptRoles(){
		PrintWriter out = PubMethod.GetPrintWriter(); 
		try {
			String roles = this.getNextSteptRolesBase();
			out.print("{\"result\":\"success\",\"roles\":\""+roles+"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\""+e.getMessage()+"\"}");
		}
	}
	
	public static void copyFile(File sourceFile, File targetFile) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        } finally {
            // 关闭流
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    }
	
	public Junyans getJunyans() {
		return junyans;
	}
	public void setJunyans(Junyans junyans) {
		this.junyans = junyans;
	}
	public EquipmentService getEpService() {
		return epService;
	}
	public void setEpService(EquipmentService epService) {
		this.epService = epService;
	}
	public JunyansService getJunyansService() {
		return junyansService;
	}
	public void setJunyansService(JunyansService junyansService) {
		this.junyansService = junyansService;
	}

	public ICodeReviewService getCodeReviewService() {
		return codeReviewService;
	}

	public void setCodeReviewService(ICodeReviewService codeReviewService) {
		this.codeReviewService = codeReviewService;
	}
}
